package 晚间作业0316.Doc11_实战案例一;

import javax.swing.*;
import java.util.Random;

public class Doc11 {

    /**
     * 使用自定义异常解决
     * 题目:老师使用电脑上课 上课时电脑可能发生蓝屏异常---重启电脑 死机异常---老师上课异常-----去苹果店修电脑
     */
    public static void main(String[] args) {
        // 实战案例一
        Teacher teacher = new Teacher("张老师");
        try {
            teacher.teach(new Computer());
        } catch (ClassTeachingException e) {
            System.out.println("去苹果店修电脑");
        }
    }

    public static class Computer {

        public void reset() {
            System.out.println("重启，正常工作中...");
        }

        public void work() throws ComputerExceptions.BlueScreenException, ComputerExceptions.MNRException {
            Random random = new Random();
            int i = random.nextInt(3);
            switch (i) {
                case 0: //正常工作
                    System.out.println("正常工作中...");
                    break;
                case 1://蓝屏异常
                    throw new ComputerExceptions.BlueScreenException();
                case 2://死机异常
                    throw new ComputerExceptions.MNRException();
            }
        }
    }

    public static class Teacher {
        private String name;

        public Teacher(String name) {
            this.name = name;
        }

        public void teach(Computer computer) throws ClassTeachingException{
            try {
                computer.work();
            } catch (ComputerExceptions.BlueScreenException e) {
                System.out.println(e.getMessage());
                System.out.println(name + "重启电脑");
                computer.reset();
            } catch (ComputerExceptions.MNRException e) {
                System.out.println("没法上课了，因为" + e.getMessage());
                throw new ClassTeachingException();
            }
        }
    }

    public static class ClassTeachingException extends Exception{
        public ClassTeachingException() {
            this("上课异常");
        }

        public ClassTeachingException(String message) {
            super(message);
        }
    }
}
